#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"../include/main.h"
#include"../include/movefile.h"
#include"../include/mount_vfs.h"
#include"../include/narrtree.h"
#include"../include/hash.h"
#include"../include/bst.h"
#include"../include/create_vfs.h"
#include"../include/vfs_errorcodes.h"
#include"../include/constants.h"
#include"../include/deldir.h"

int found_1=0;
int found_2=0,d;
ntree *prev1;
fdesc *mdesc,*todelete;
char *name;
void movefile(char filepath1[],char filepath2[])
{ 
	ntree *tofind1,*tofind2;
	char *pch,*tp,fpath[MAX_PATH];
	int i=0;
	llist *list;
	schflag=0;
	tofind1=search_node(head,filepath1);
	schflag=0;
	tofind2=search_node(head,filepath2);
	schflag=0;
        if(mounted==0)
		printf("%s_FAILURE %s\n",glb_command,ERR_VFS_MOVEFILE_06);    
	else if(tofind1==NULL)
		printf("%s_FAILURE %s\n",glb_command,ERR_VFS_MOVEFILE_01);
	else if(tofind1->file_desc->filetype==1)
		printf("%s_FAILURE %s\n",glb_command,ERR_VFS_MOVEFILE_01);
	else if(tofind2==NULL)
	{
		pch=strrchr(filepath2,'/');
		if(pch != NULL)
		{
			tp=filepath2;
			while(tp != pch)
			{
				fpath[i++]=*tp;
				tp++;
			}
			fpath[i]='\0';
			tofind2=search_node(head,fpath);
		}
		else
			tofind2=search_node(head,filepath2);

		if(tofind2==NULL)
			printf("%s_FAILURE %s\n",glb_command,ERR_VFS_MOVEFILE_02);
		else
		{
			//printf("tofind1->file_desc->filepath==%s\n",tofind1->file_desc->filepath);
			todelete = (fdesc *)malloc(sizeof(fdesc));
			strcpy(todelete->filepath,tofind1->file_desc->filepath);
			strcpy(todelete->filename,tofind1->file_desc->filename);
			todelete->filetype=tofind1->file_desc->filetype;		
			todelete->blk_num=tofind1->file_desc->blk_num;
			todelete->file_size=tofind1->file_desc->file_size;
			mdesc = (fdesc *)malloc(sizeof(fdesc));
			mdesc->blk_num = tofind1->file_desc->blk_num;
			strcpy(mdesc->filepath,filepath2);
			name = strrchr(filepath2,'/');
			name++;
			strcpy(mdesc->filename,name);
			mdesc->filetype=tofind1->file_desc->filetype;
			mdesc->file_size=tofind1->file_desc->file_size;
			head = insert_ntree(head,mdesc);
			list=insert(mdesc);
			//deldir(todelete->filepath);
			head = delete_ntree(head,tofind1->file_desc);
			d = deletion(todelete);
			//move(filepath1,filepath2);
			printf("movefile_SUCCESS\n");
		}
	}
	else if(tofind2->file_desc->filetype==0)
        {
		remove_file(filepath2);
		todelete = (fdesc *)malloc(sizeof(fdesc));
		strcpy(todelete->filepath,tofind1->file_desc->filepath);
		strcpy(todelete->filename,tofind1->file_desc->filename);
		todelete->filetype=tofind1->file_desc->filetype;		
		todelete->blk_num=tofind1->file_desc->blk_num;
		todelete->file_size=tofind1->file_desc->file_size;
		mdesc = (fdesc *)malloc(sizeof(fdesc));
		mdesc->blk_num = tofind1->file_desc->blk_num;
		strcpy(mdesc->filepath,filepath2);
		name = strrchr(filepath2,'/');
		name++;
		strcpy(mdesc->filename,name);
		mdesc->filetype=tofind1->file_desc->filetype;
		mdesc->file_size=tofind1->file_desc->file_size;
		head = insert_ntree(head,mdesc);
		list=insert(mdesc);
		//deldir(todelete->filepath);
		
		head = delete_ntree(head,tofind1->file_desc);
		//printf("tofind1->file_desc->filepath==%s\n",tofind1->file_desc->filepath);
		d = deletion(todelete);
		//d = deletion(tofind1->file_desc);
		printf("movefile_SUCCESS\n");
	}
	else
	{
		move(filepath1,filepath2);
	}
	//traverse(head);
}

void move(char filepath1[],char filepath2[])
{
	ntree *parent1=NULL,*parent2=NULL,*temp;
	char upath[MAX_PATH],*pch;
	parent1 = search_first(head,filepath1);
	parent2 = search_second(head,filepath2);
	if(parent1 != NULL)
	{
		if(parent1->next != NULL)
		if(!(strcmp(parent1->next->file_desc->filepath,filepath1)))
		{
			temp=parent1->next;
			strcpy(upath,parent2->file_desc->filepath);
			pch=strrchr(temp->file_desc->filepath,'/');
			strcat(upath,pch);
			strcpy(temp->file_desc->filepath,upath);
			if(parent2->first == NULL)
			{
				parent2->first = parent1->next ;
				parent1->next = parent1->next->next;
				parent2->first->next=NULL;
			}
			else
			{
				parent2=parent2->first;
				while(parent2->next != NULL)
					parent2=parent2->next;	
				parent2->next = parent1->next ;	
				parent1->next = parent1->next->next;
				parent2->next->next=NULL;	
			}
			printf("movefile_SUCCESS\n");		
		}
		if(parent1->first != NULL)
		if(!(strcmp(parent1->first->file_desc->filepath,filepath1)))
		{
			temp=parent1->first;
			strcpy(upath,parent2->file_desc->filepath);
			pch=strrchr(temp->file_desc->filepath,'/');
			strcat(upath,pch);
			strcpy(temp->file_desc->filepath,upath);
			if(parent2->first == NULL)
			{
				parent2->first = parent1->first ;
				parent1->first = parent1->first->next;
				parent2->first->next=NULL;
			}
			else
			{
				parent2=parent2->first;
				while(parent2->next != NULL)
					parent2=parent2->next;	
				parent2->next = parent1->first ;
				parent1->first = parent1->first->next;
				parent2->next->next=NULL;	
			}
			printf("movefile_SUCCESS\n");
		}
	}
}

ntree *search_first(ntree * root, char filepath1[])
{
	ntree *parent,*temp,*ans,*in_temp;
	//ntree *prev,*prev1;
	temp = root;
	if(temp!=NULL)
	{
		parent=temp;
		do
		{
			if((parent != NULL)&&(found_1==0))
			{
				if(!strcmp(parent->file_desc->filepath, filepath1))
				{
					if(prev1->next != NULL)
					if(!strcmp(prev1->next->file_desc->filepath,filepath1))			
						parent = prev1;
					if(prev1->first != NULL)
					if(!strcmp(prev1->first->file_desc->filepath,filepath1))
						parent = prev1;
					found_1 = 1;
					//printf("\nsearch1 Returning parentnode    parent->file_desc->filepath==%s\n",parent->file_desc->filepath);
					return parent;
				}
			}
			prev1=parent;
			ans=search_first(parent->first,filepath1);
			if(found_1==1)
				return ans;
			prev1=parent;
			parent=parent->next;
		}while(parent!=NULL);
		temp=temp->first;
	}
}


ntree *search_second(ntree * root, char filepath2[])
{
	ntree *parent,*temp,*ans;
	ntree *prev;
	temp = root;
	if(!strcmp(filepath2,"/"))
	{
		//printf("\nsearch2 Returning node    root->file_desc->filepath==%s\n",root->file_desc->filepath);
		return root;
	}
	if(temp!=NULL)
	{
		parent=temp;
		do
		{
			if((parent != NULL)&&(found_2==0))
			{
				if(!strcmp(parent->file_desc->filepath, filepath2))
				{
					found_2 = 1;
					//printf("\nsearch2 Returning parentnode    parent->file_desc->filepath==%s\n",parent->file_desc->filepath);
					return parent;
				}
			}
			prev=parent;
			ans=search_second(parent->first,filepath2);
			if(found_2==1)
				return ans;
			parent=parent->next;
		}while(parent!=NULL);
		temp=temp->first;
	}
}
